﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using memcached;

namespace CacheExamples
{
    public class Program
    {
        public static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();

            try
            {
                // requires default (passwordless) Redis client running on localhost
                using (RedisCache c = new RedisCache())
                {
                    bool res = c.Set("test", "value");
                    res = c.Add("test", 100, "v1");
                    bool s;
                    object v = c.Get("test", out s);
                    System.Diagnostics.Debug.Assert("value".Equals(v));
                    v = c.Get("test1", out s);
                    System.Diagnostics.Debug.Assert(v == null);
                    res = c.Remove("test");
                    System.Diagnostics.Debug.Assert(res);
                    long v1 = c.Increment("inck", 2);
                    v1 = c.Decrement("inck");
                    System.Diagnostics.Debug.Assert(v1 == 1);

                    using (c.Lock("mylock", 3))
                    {
                        res = c.Add("newk", 1, "test");
                        System.Diagnostics.Debug.Assert(res);
                        System.Threading.Thread.Sleep(1500);
                        string r = c.Get<string>("newk", out res);
                        System.Diagnostics.Debug.Assert(r == null);
                    }
                    Console.WriteLine("Redis test succeeded");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            // requires client.ncconf in NCache directory
            try
            {
                using (NCache c = new NCache())
                {
                    bool res = c.Set("test", "value");
                    res = c.Add("test", 100, "v1");
                    bool s;
                    object v = c.Get("test", out s);
                    System.Diagnostics.Debug.Assert("value".Equals(v));
                    v = c.Get("test1", out s);
                    System.Diagnostics.Debug.Assert(v == null);
                    res = c.Remove("test");
                    System.Diagnostics.Debug.Assert(res);
                    Console.WriteLine("NCache test succeeded");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                Console.WriteLine("Starting memcached performance test..");
                PerfTest.RunTest();

                SyncTest.RunTest();

                Console.WriteLine("Starting memcached load test..");
                RunLoadTest();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("Press Enter to finish");
            Console.ReadLine();
        }

        // try to estimate maxium number of memcached clients at once
        private static void RunLoadTest()
        {
            List<MemCache> l = new List<MemCache>(6000);
            int i = 0;
            try
            {
                for (i = 0; i < l.Capacity; i++)
                {
                    System.Threading.Thread.Sleep(50);
                    var c = new MemCache();
                    l.Add(c);
                    if (!c.Set("val" + i, "test value" + i))
                        break;
                    if ((i % 100) == 0)
                        Console.WriteLine("Allocated: " + i);
                }
            }
            catch
            {
            }
            finally
            {
                //>1900
                Console.WriteLine("Maximum supported connections: " + i);
                System.Threading.Thread.Sleep(10 * 1000);
                for (i = 0; i < l.Count; i++)
                {
                    MemCache c = l[i];
                    bool x;
                    var s = c.Get<string>("val" + i, out x);
                    c.Dispose();
                }
                Console.WriteLine("Perf test finished");
            }
        }
    }
}